from pathlib import Path

from gemini_antenna.multiFeedParsing import (parsegemsub, parsetwtxt, parsexml,
                                             FeedEntry, TwtxtEntry)


def test_gemsub():
    with open(Path("tests") / "feed.gmi") as file:
        feed = file.read()
    entries = parsegemsub(feed, "gemini://gemini.jrandom.net/gemlog/")
    assert entries == [
        FeedEntry(
            feedurl="gemini://gemini.jrandom.net/gemlog/",
            author="J. Random Geminaut's gemlog",
            updated=1605855600,
            title="- Early Bokashi composting experiments",
            link="gemini://gemini.jrandom.net/gemlog/bokashi.gmi"
        ),
        FeedEntry(
            feedurl="gemini://gemini.jrandom.net/gemlog/",
            author="J. Random Geminaut's gemlog",
            updated=1605250800,
            title="- Trying to get to grips with finite simple groups...",
            link="gemini://gemini.jrandom.net/gemlog/finite-simple-groups.gmi"
        ),
        FeedEntry(
            feedurl="gemini://gemini.jrandom.net/gemlog/",
            author="J. Random Geminaut's gemlog",
            updated=1604646000,
            title="- I started a balcony garden!",
            link="gemini://gemini.jrandom.net/gemlog/balcony.gmi"
        )
    ]


def test_atom():
    with open(Path("tests") / "feed.atom") as file:
        feed = file.read()
    entries = parsexml(feed, "gemini://gemini.jrandom.net/gemlog/")
    assert entries == [
        FeedEntry(
            feedurl="gemini://gemini.jrandom.net/gemlog/",
            author="J. Random Geminaut's gemlog",
            updated=1605855600,
            title="Early Bokashi composting experiments",
            link="gemini://gemini.jrandom.net/gemlog/bokashi.gmi"
        ),
        FeedEntry(
            feedurl="gemini://gemini.jrandom.net/gemlog/",
            author="J. Random Geminaut's gemlog",
            updated=1605250800,
            title="Trying to get to grips with finite simple groups...",
            link="gemini://gemini.jrandom.net/gemlog/finite-simple-groups.gmi"
        ),
        FeedEntry(
            feedurl="gemini://gemini.jrandom.net/gemlog/",
            author="J. Random Geminaut's gemlog",
            updated=1604646000,
            title="I started a balcony garden!",
            link="gemini://gemini.jrandom.net/gemlog/balcony.gmi"
        )
    ]


def test_twtxt():
    with open(Path("tests") / "twtxt.txt") as file:
        feed = file.read()
    entries = parsetwtxt(feed, "gemini://example.com/twtxt.txt")
    assert entries == [
        TwtxtEntry(
            feedurl="gemini://example.com/twtxt.txt",
            author="gemini://example.com/twtxt.txt",
            posted=1454589000.0,
            twt="You can really go crazy here! ┐(ﾟ∀ﾟ)┌"
        ),
        TwtxtEntry(
            feedurl="gemini://example.com/twtxt.txt",
            author="gemini://example.com/twtxt.txt",
            posted=1454537100.0,
            twt="@<example http://example.org/twtxt.txt> welcome to twtxt!"
        ),
        TwtxtEntry(
            feedurl="gemini://example.com/twtxt.txt",
            author="gemini://example.com/twtxt.txt",
            posted=1454320800.0,
            twt="This is just another example."
        ),
        TwtxtEntry(
            feedurl="gemini://example.com/twtxt.txt",
            author="gemini://example.com/twtxt.txt",
            posted=1449918000.0,
            twt="Fiat lux!"
        )
    ]
